<template>
    <div>
        <scroller
            :on-refresh="refresh"
            :on-infinite="infinite"
            ref="infinite"
        >
            <div class="cont">
                <router-link
                    :to="{path:'/couponCont',query:{coupon_id: item.id}}"
                    v-for="(item, index) in listSource"
                    :key="index"
                    class="list"
                    :style=" index === 0 ? 'margin-top:0px' : '' "
                >
                    <img :src="item.images">
                    <div>
                        <p>{{item.title}}</p>
                        <div>
                            <div>
                                <p>￥{{item.fee}}</p>
                                <p>￥{{item.o_price}}</p>
                            </div>
                            <div :class="item.return_money_msg =='立即购买'?'hotShop':'hotMoney'">
                                {{item.return_money_msg}}
                            </div>
                        </div>
                    </div>
                </router-link>
            </div>
        </scroller>
    </div>
</template>
<script>
import wxGetAddress from '../../config/wxGetAddress.js';
export default {
    name: "couponSearch",
    data() {
        return {
            page: 1,
            limit: 10,
            noData: true,
            iscur: 0,//选择 索引
            lat: 35.95995,
            lon: 120.19653,
            listSource: [],
        };
    },
    methods: {
        infinite() {
            console.log('infinite1');
            this.listData();
        },
        refresh() {
            this.page = 1;
            this.listData(true);
        },
        listData(type) {
            let params = {
                page: this.page,
                limit: this.limit,
            };
            let url = '/coupon/get_list'
            if (this.$route.query.type == 'classify') {
                params.cate_id = this.$route.query.id
            }
            if (this.$route.query.type == 'hot') {
                params.type = 'hot'
            }
            if (this.$route.query.type == 'goods') {
                params.type = 'best_goods'
            }
            if (this.$route.query.type == 'address') {
                params.lat = this.lat
                params.lng = this.lon
            }
            this.$post(url, params)
                .then(res => {
                    if (res.code === 20000) {
                        this.page++;
                        if (type) {
                            this.listSource = res.data.list;
                            setTimeout(() => {
                                this.$refs.infinite.finishPullToRefresh();
                            }, 500);
                        } else {
                            for (let i in res.data.list) {
                                let newData = res.data.list[i];
                                this.listSource.push(newData);
                            }
                            if (res.data.list.length < this.limit) {
                                this.noData = false;
                                this.$refs.infinite.finishInfinite(true);
                            } else {
                                this.$refs.infinite.finishInfinite();
                            }
                        }
                    }
                })
                .catch(err => { });
        },
        getWXConfig() {
            let params = {
                url: 'http://wangzhuan.3todo.cn/',
            };
            this.$post("/index/share", params)
                .then(res => {
                    if (res.code === 20000) {
                        wxGetAddress(res.data, this.wxGetAddressSuccess)
                    }
                })

        },
        wxGetAddressSuccess(lat, lon) {
            this.lat = lat
            this.lon = lon
            this.refresh()
        }
    },
    beforeCreate() {
        document.querySelector("body").setAttribute("style", "background-color:#f0effe");
    },
    created() {
        // 实例创建完成
        // this.searchData();
        if (this.$route.query.type == 'address') {
            this.getWXConfig()
        }
    },
};
</script>
<style lang="less" scoped>
@import url("../../asset/css/couponList");
</style>